package jp.tonosama.komoki.SimpleGolfScorer2.main;

import jp.tonosama.komoki.SimpleGolfScorer2.R;
import jp.tonosama.komoki.SimpleGolfScorer2.data.GolfScoreData;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

/**
 * @author Komoki
 */
public class DragUi {

    /**  */
    private boolean mIsNextAnim;
    /**  */
    private boolean mIsAnimating;
    /**  */
    private int mMoveValue = 1;
    /**  */
    private int mOffsetX;
    /**  */
    private int mCurrentX = 5;
    /**  */
    public static final int DEFAULT_X = 5;

    public int getCurrentX() {
        return mCurrentX;
    }

    public void setCurrentX(final int currentX) {
        this.mCurrentX = currentX;
    }

    public boolean isAnimating() {
        return mIsAnimating;
    }

    public void setIsAnimating(final boolean isAnimating) {
        this.mIsAnimating = isAnimating;
    }

    public boolean isNextAnim() {
        return mIsNextAnim;
    }

    public void setIsNextAnim(final boolean isNextAnim) {
        this.mIsNextAnim = isNextAnim;
    }

    public int getMoveValue() {
        return mMoveValue;
    }

    public void setMoveValue(final int moveValue) {
        this.mMoveValue = moveValue;
    }

    /**
     * フッターエリアをタッチ&ドラッグ動作
     */
    public void setFooterAreaAction(final Activity activity, final GolfScoreData mScoreData,
            final View mFooterArea, final ImageView mPrevArrw, final ImageView mNextArrw,
            final View mDragImag, final View mCurrentPosImg) {

        mFooterArea.setOnTouchListener(new View.OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            public boolean onTouch(final View v, final MotionEvent event) {
                int x = (int) event.getX() - mPrevArrw.getWidth()
                        - (mCurrentPosImg.getWidth() / 14);
                int holeLength = mDragImag.getMeasuredWidth() / 20;
                // ドラッグしたら
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    int diffX = (mOffsetX - x) * 1;
                    setCurrentX(getCurrentX() - diffX);
                    mOffsetX = x;
                    if (getCurrentX() < 0) {
                        setCurrentX(0);
                        mOffsetX = 0;
                    }
                    if (getCurrentX() > holeLength * 18) {
                        setCurrentX(holeLength * 18);
                        mOffsetX = holeLength * 18;
                    }
                    actionMoveMain(activity, mScoreData, holeLength, mCurrentPosImg);
                } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    // タッチダウンしたら
                    actionDownMain(x, mPrevArrw, mNextArrw, mDragImag, mCurrentPosImg);
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    // タッチアップしたら
                    actionUpMain(activity, mScoreData, holeLength);
                }
                return true;
            }
        });
    }

    /**
     * @param activity Activity
     * @param mScoreData GolfScoreData
     * @param holeLength int
     * @param mCurrentPosImg View
     */
    private void actionMoveMain(final Activity activity, final GolfScoreData mScoreData,
            final int holeLength, final View mCurrentPosImg) {

        int tmpHole = getTmpHole(holeLength);
        mCurrentPosImg.setVisibility(View.VISIBLE);
        mCurrentPosImg.layout(getCurrentX(), 0, getCurrentX() + mCurrentPosImg.getWidth(),
                mCurrentPosImg.getHeight());
        if (mScoreData.getIs18Hround()) {
            ((ImageView) activity.findViewById(R.id.golf_hole_icon))
                    .setImageResource(R.drawable.golf_hole_icon);
            MainRes.getHoleTitleTextView(activity).setBackgroundResource(
                    MainRes.HOLE_NUMBER_IMG_RES_IDS[tmpHole - 1]);
        } else {
            if (tmpHole < 10) {
                ((ImageView) activity.findViewById(R.id.golf_hole_icon))
                        .setImageResource(R.drawable.golf_hole_icon_out);
                MainRes.getHoleTitleTextView(activity).setBackgroundResource(
                        MainRes.HOLE_NUMBER_IMG_RES_IDS[tmpHole - 1]);
            } else {
                ((ImageView) activity.findViewById(R.id.golf_hole_icon))
                        .setImageResource(R.drawable.golf_hole_icon_in);
                MainRes.getHoleTitleTextView(activity).setBackgroundResource(
                        MainRes.HOLE_NUMBER_IMG_RES_IDS[tmpHole - 10]);
            }
        }
    }

    /**
     * @param x int
     * @param mPrevArrw ImageView
     * @param mNextArrw ImageView
     * @param mDragImag View
     * @param mCurrentPosImg View
     */
    private void actionDownMain(final int x, final ImageView mPrevArrw, final ImageView mNextArrw,
            final View mDragImag, final View mCurrentPosImg) {
        mPrevArrw.setImageResource(R.drawable.arrow_leftside_touch);
        mNextArrw.setImageResource(R.drawable.arrow_rightside_touch);
        mOffsetX = x;
        setCurrentX(x);
        mDragImag.setVisibility(View.INVISIBLE);
        mCurrentPosImg.setVisibility(View.INVISIBLE);
        mCurrentPosImg.layout(getCurrentX(), 0, getCurrentX() + mCurrentPosImg.getWidth(),
                mCurrentPosImg.getHeight());
    }

    /**
     * @param activity Activity
     * @param mScoreData GolfScoreData
     * @param holeLength int
     */
    private void actionUpMain(final Activity activity, final GolfScoreData mScoreData,
            final int holeLength) {
        int tmpHole = getTmpHole(holeLength);
        if (mScoreData.getCurrentHole() == tmpHole) {
            ((MainGolfScorer) activity).refreshEditor(mScoreData, 0);
        } else if (mScoreData.getCurrentHole() > tmpHole) {
            // アニメーション対応
            // (右から中心,中心から右方向,下から中心,中心から下方向)
            TranslateAnimation trans = new TranslateAnimation(0, 400, 0, 0);
            AlphaAnimation alpha = new AlphaAnimation(1, 0);
            trans.setDuration(100);
            alpha.setDuration(100);
            trans.setInterpolator(new DecelerateInterpolator());
            alpha.setInterpolator(new AccelerateInterpolator());
            AnimationSet set = new AnimationSet(true);
            set.addAnimation(trans);
            set.addAnimation(alpha);
            set.setAnimationListener((AnimationListener) activity);
            setIsNextAnim(false);
            setMoveValue(tmpHole - mScoreData.getCurrentHole());
            setIsAnimating(true);
            activity.findViewById(MainRes.DRUM_PICKER_AREA_RES_ID).startAnimation(set);
        } else if (mScoreData.getCurrentHole() < tmpHole) {
            // アニメーション対応
            // (右から中心,中心から右方向,下から中心,中心から下方向)
            TranslateAnimation trans = new TranslateAnimation(0, -400, 0, 0);
            AlphaAnimation alpha = new AlphaAnimation(1, 0);
            trans.setDuration(100);
            alpha.setDuration(100);
            trans.setInterpolator(new DecelerateInterpolator());
            alpha.setInterpolator(new AccelerateInterpolator());
            AnimationSet set = new AnimationSet(true);
            set.addAnimation(trans);
            set.addAnimation(alpha);
            set.setAnimationListener((AnimationListener) activity);
            setIsNextAnim(true);
            setMoveValue(tmpHole - mScoreData.getCurrentHole());
            setIsAnimating(true);
            activity.findViewById(MainRes.DRUM_PICKER_AREA_RES_ID).startAnimation(set);
        }
    }

    /**
     * @param holeLength int
     * @return tmpHole
     */
    private int getTmpHole(final int holeLength) {
        int tmpHole = getCurrentX() / holeLength + 1;
        if (tmpHole < 1) {
            tmpHole = 1;
        }
        if (tmpHole > 18) {
            tmpHole = 18;
        }
        return tmpHole;
    }
}